package com.wlw.admin.openeye.ui.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.support.v4.app.ActivityCompat
import android.support.v4.app.ActivityOptionsCompat
import android.support.v4.util.Pair
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.hazz.kotlinmvp.view.recyclerview.ViewHolder
import com.hazz.kotlinmvp.view.recyclerview.adapter.CommonAdapter
import com.wlw.admin.openeye.Constants
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.durationFormat
import com.wlw.admin.openeye.mvp.model.bean.HomeBean
import com.wlw.admin.openeye.ui.activity.VideoDetailActivity

class FollowHorizontalAdapter(context: Context, data: ArrayList<HomeBean.Issue.Item>) : CommonAdapter<HomeBean.Issue.Item>(context, data, R.layout.item_child_follow) {
    override fun bindData(holder: ViewHolder, data: HomeBean.Issue.Item, position: Int) {
        val horizontalItemData = data.data
        Glide.with(mContext).load(horizontalItemData?.cover?.feed)
                .transition(DrawableTransitionOptions().crossFade())
                .into(holder.getView(R.id.iv_cover_feed))

        horizontalItemData?.title?.let { holder.setText(R.id.tv_title, it) }
        val timeFormat = durationFormat(horizontalItemData?.duration)

        with(holder) {
            if (horizontalItemData?.tags != null && horizontalItemData.tags.size > 0)
                setText(R.id.tv_tag, "#${horizontalItemData.tags[0].name}/$timeFormat")
            else
                setText(R.id.tv_tag, "#$timeFormat")
        }
    holder.setOnItemClickListener(View.OnClickListener {
        goToVideoPlayer(mContext as Activity, holder.getView(R.id.iv_cover_feed), data)
    })

}

override fun getItemCount(): Int = mData.size

private fun goToVideoPlayer(activity: Activity, iv_cover_feed: ImageView, itemData: HomeBean.Issue.Item) {
    val intent = Intent(activity, VideoDetailActivity::class.java)
    intent.putExtra(Constants.BUNDLE_VIDEO_DATA, itemData)
    intent.putExtra(VideoDetailActivity.TRANSITION, true)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        val pair = Pair<View, String>(iv_cover_feed, VideoDetailActivity.IMG_TRANSITION)
        val activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pair)
        ActivityCompat.startActivity(activity, intent, activityOptions.toBundle())
    } else {
        activity.startActivity(intent)
        activity.overridePendingTransition(R.anim.anim_in, R.anim.anim_out)
    }
}
}